import axios from "axios";
import router from "../router"

export const baseURL = "/";
const instance = axios.create({
    baseURL,
    timeout: 5000,
});

// header携带token
instance.interceptors.request.use(function(config) {
    // Do something before request is sent
    // const token = localStorage.getItem("token");
    // if (token) {
    //     config.headers['Authorization'] = 'Bearer ' + token
    // }
    // console.log("拦截器生效", config.headers.Authorization);
    return config;
}, function(error) {
    // Do something with request error
    return Promise.reject(error);
});

// 响应拦截 token过期拦截
axios.interceptors.response.use(res => {
    if (res.data.code === 401) {
        router.replace('/')
        localStorage.removeItem("token")
    }
    return res;
})


export default instance;